/*
 * VHGwtRpcController.java
 * Created on 7 févr. 2009
 *
 * Glacéo Internet Platform
 * http://sourceforge.net/projects/chm/
 *
 * Copyright (c) 2005-2009, Jens and Jan Janke (VirtualHockey Project)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/.
 */

package com.hockeo.vhweb.server.servlet.base;

import java.lang.reflect.InvocationTargetException;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.context.ServletContextAware;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.google.gwt.user.client.rpc.SerializationException;
import com.google.gwt.user.server.rpc.RPC;
import com.google.gwt.user.server.rpc.RPCRequest;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * Main controller for GWT/RPC requests.
 *
 * <p>
 * Implemented according to instructions from the following excellent (many thanks)
 * article on <a
 * href="http://technophiliac.wordpress.com/2008/08/24/giving-gwt-a-spring-in-its-step/"
 * >Technophiliac</a>.
 * </p>
 *
 * @version $Id$
 * @author jjanke
 */
public class VHGwtRpcController extends RemoteServiceServlet implements ServletContextAware, Controller
{
  private ServletContext    d_servletContext;
  private VHAbstractService d_remoteService;

  public ModelAndView handleRequest( HttpServletRequest request, HttpServletResponse response ) throws Exception
  {
    super.doPost( request, response );
    return null;
  }

  @Override
  public String processCall( String strPayLoad ) throws SerializationException
  {
    try
    {
      // Provide the service with access to the last HTTP request
      //
      d_remoteService.setThreadLocalRequest( getThreadLocalRequest() );

      // Invoke the right method using reflection
      //
      RPCRequest rpcRequest = RPC.decodeRequest( strPayLoad, d_remoteService.getClass(), this );
      Object objResult = rpcRequest.getMethod().invoke( d_remoteService, rpcRequest.getParameters() );

      // Encode response for the client
      //
      return RPC.encodeResponseForSuccess( rpcRequest.getMethod(), objResult, rpcRequest.getSerializationPolicy() );
    }
    catch ( InvocationTargetException ex )
    {
      getServletContext().log( "Invocation target expection.", ex.getCause() );
      return RPC.encodeResponseForFailure( null, ex.getCause() );
    }
    catch ( Exception ex )
    {
      getServletContext().log( "Server side request processing failed.", ex );
      return RPC.encodeResponseForFailure( null, ex );
    }
    finally
    {
      // Remove the request from the servlet
      //
      d_remoteService.removeThreadLocalRequest();
    }
  }

  public void setRemoteService( VHAbstractService remoteService )
  {
    d_remoteService = remoteService;
  }

  public final void setServletContext( ServletContext context )
  {
    d_servletContext = context;
  }

  @Override
  public final ServletContext getServletContext()
  {
    return d_servletContext;
  }
}
